//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/painter/ring_painter.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';

/// 正确率组件
class ResultAccuracyWidget extends StatelessWidget {
  /// 正确率
  final int accuracy;

  /// 试卷名称
  final String paperName;

  /// 交卷时间
  final String date;

  const ResultAccuracyWidget({
    super.key,
    required this.accuracy,
    required this.paperName,
    required this.date,
  });

  @override
  Widget build(BuildContext context) {

    return Container(
      height: 408,
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.all(Radius.circular(20)),
      ),
      child: Stack(
        children: [
          _ringItem(),
          _ringGradient(),
          _accuracyItem(),
          _bottomItem(),
        ],
      ),
    );
  }

  /// 底部圆环指示器
  _ringItem() {
    return Positioned.fill(
      top: 0,
      // left: 150,
      // right: 150,
      // bottom: -12,
      // child:Container(),
      child: CustomPaint(
        // size: Size(1.sw-40, 390),
        // size: Size(1.sw-32, 390),
        painter: RingPainter(accuracy: accuracy / 100),
        child: Padding(
          padding: EdgeInsets.fromLTRB(30, 30, 30, 194),
          child: SvgPicture.asset(
            "images/question/result_intersect.svg",
            width: 1.sw-100,
            height: 165,
          ),
        ),
      ),
    );
  }

  /// 圆形渐变
  _ringGradient() {
    return Positioned(
      top: 94,
      left:  0.5.sw-110-20,
      child: Container(
        width: 220,
        height: 220,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(131)),
          gradient: const RadialGradient(
            center: Alignment(0, -0.2),
            colors: [colorBlue25, colorBlue2, colorWhite1],
            stops: [0, 0.5, 1],
          ),
        ),
      ),
    );
  }

  /// 正确率
  _accuracyItem() {
    return Positioned(
      top: 124,
      left: 0.5.sw-60-20,
      child: SizedBox(
        width: 120,
        height: 105,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              "$accuracy%",
              style: FontUtil.fontBlack1(46),
            ),
            Text(
              "正确率",
              style: FontUtil.fontBlack1(24),
            ),
          ],
        ),
      ),
    );
  }

  /// 底部试卷名称、交卷时间显示
  _bottomItem() {
    return Positioned(
      top: 280,
      left: 10,
      child: SizedBox(
        width: 1.sw-32-10,
        height: 88,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _showBottomItem(
              svgUrl: "images/question/result_accuracy_name.svg",
              title: "试卷名称：",
              value: paperName,
            ),
            _showBottomItem(
              svgUrl: "images/question/result_accuracy_date.svg",
              title: "交卷时间：",
              value: date,
            ),
          ],
        ),
      ),
    );
  }

  /// 底部显示样式
  _showBottomItem({
    required String svgUrl,
    required String title,
    required String value,
  }) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            SvgPicture.asset(
              svgUrl,
              width: 24,
              height: 26,
            ),
            hiSpace(width: 10),
            Text(
              title,
              style: FontUtil.fontGrey1(20),
            ),
          ],
        ),
        Expanded(
          child: Text(
            value,
            style: FontUtil.fontGrey1(16),
            textAlign: TextAlign.end,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }
}
